<template>
  <div class="good">
    <van-card class="goodcard" :thumb="selectgood.image">
      <template #title>
        <div class="title">{{ selectgood.suk }}</div>
      </template>
      <template #desc>
        <div class="qian">
          <span class="qian-icon">¥</span>{{ selectgood.price }}
        </div>
      </template>
      <template #tags>
        <div class="kucun">库存:{{ selectgood.stock }}</div>
      </template>
    </van-card>

    <div
      class="valueattr"
      v-for="(item, index) in some.productAttr"
      :key="index"
    >
      <p>{{ item.attrName }}</p>
      <div class="nmd">
        <div
          class="qnmd"
          v-for="(value, dot) in item.attrValues"
          :key="dot"
          :class="subIndex[index] == dot ? 'active' : ''"
          @click="selectthis(value, index, $event, dot)"
        >
          {{ value }}
        </div>
      </div>
    </div>

    <div class="shuliang">
      <span class="text">数量</span>
      <van-stepper class="jjjj" v-model="count" />
    </div>
    <van-button class="yesbtn" round type="info" @click="addCart"
      >确定</van-button
    >
  </div>
</template>
<script>
import axios from "axios";
export default {
  props: ["goodid"],
  data() {
    return {
      count: 1,
      some: [],
      selectArr: [],
      id: this.goodid,
      someValue: [],
      someAttr: [],
      selectgood: [],
      // isActive: 0,
      subIndex: [],
      shopItemInfo: {},
    };
  },
  created() {
    // console.log(this.goodid);
    this.getSome();
    // this.checkItem();
  },
  methods: {
    addCart() {},
    selectthis(value, index, event, dot) {
      this.selectArr[index] = value;
      this.subIndex[index] = dot;

      // if (this.selectArr[index]!==value) {
      //   this.selectArr[index]=value;
      //   this.subIndex[index]=dot;
      // }
      // else{
      //   this.selectArr[index]="";
      // this.subIndex[index]=-1
      // }
      console.log(this.selectArr);
      this.checkItem(dot);
    },
    checkItem(dot) {
      this.isActive = dot;
      // console.log(this.subIndex);

      var option = this.some.productAttr;
      var result = [];

      // console.log(this.selectgood);
      for (var i in option) {
        result[i] = this.selectArr[i] ? this.selectArr[i] : "";
      }
      for (var i in option) {
        var last = result[i];
        for (var j in option[i].attrValues) {
          result[i] = option[i].attrValues[j];
        }
        result[i] = last;
      }
      if (this.shopItemInfo[result]) {
        this.selectgood = this.shopItemInfo[result] || "";
      }
      console.log(this.selectgood);
      this.$forceUpdata;
    },

    async getSome() {
      // const res = await this.$http.getxianqin(this.goodid);
      // this.subIndex = [];
      // this.some = res.data.data;
      // this.someValue = Object.values(res.data.data.productValue);
      // this.shopItemInfo = res.data.data.productValue;
      // // console.log(this.shopItemInfo);
      // // console.log(this.someValue);
      // this.selectgood = this.someValue[0];
      // this.some.productAttr.forEach((item) => {
      //   item.attrValues = item.attrValues.split(",");
      // });
      // this.selectArr = this.selectgood.suk.split(",");

      // for (var i = 0; i < this.selectArr.length; i++) {
      //   for (var j = 0; j < this.some.productAttr[i].attrValues.length; j++) {
      //     if (this.some.productAttr[i].attrValues[j] == this.selectArr[i]) {
      //       this.subIndex[i] = j;
      //     }
      //   }
      // }
      // console.log(this.subIndex);

      await axios
        .get(
          `https://api.java.crmeb.net/api/front/product/sku/detail/${this.goodid}`
        )
        .then((res) => {
          console.log(res.data.data.productAttr);
          this.subIndex = [];
          this.count = 1;
          this.some = res.data.data;
          this.someValue = Object.values(res.data.data.productValue);
          console.log(this.someValue);
          this.shopItemInfo = res.data.data.productValue;
          // console.log(this.shopItemInfo);
          // console.log(this.someValue);
          this.selectgood = this.someValue[0];
          this.some.productAttr.forEach((item) => {
            item.attrValues = item.attrValues.split(",");
          });
          this.selectArr = this.selectgood.suk.split(",");

          for (var i = 0; i < this.selectArr.length; i++) {
            for (
              var j = 0;
              j < this.some.productAttr[i].attrValues.length;
              j++
            ) {
              if (this.some.productAttr[i].attrValues[j] == this.selectArr[i]) {
                this.subIndex[i] = j;
              }
            }
          }
          console.log(this.subIndex);
        });
    },
  },
  watch: {
    goodid: "getSome",
  },
};
</script>
<style lang="scss">
.good {
  .shuliang {
    margin: 15px 0;
    .text {
      margin: 6px 0;
      font-size: 14px;
      color: #999;
      padding: 0 14px;
    }
    .jjjj {
      float: right;
      margin-bottom: 20px;
    }
  }
  padding: 12px;
  padding-left: 2px;
  .goodcard {
    margin-right: 20px;
    background-color: white;
  }
  .nmd {
    display: flex;
    flex-wrap: wrap;

    .qnmd {
      border: 1px solid #f2f2f2;
      font-size: 12px;
      color: #282828;
      padding: 3px 16px;
      border-radius: 19px;
      margin: 9px 0 0 6px;
      background-color: #f2f2f2;
    }
    .active {
      border: 1px solid red !important;
      color: red !important;
      background-color: #fdebe9 !important;
      // font-size: 30px
    }
    // .qnmd:hover {
    //   border: 1px solid red !important;
    //   color: red !important;
    //   background-color: #fdebe9 !important;
    // }
    // .qnmd:nth-child(1){
    //    border: 1px solid red !important;
    //   color: red !important;
    //   background-color: #fdebe9 !important;
    // }
  }

  .yesbtn {
    width: 300px;
    margin-left: 35px;
    background-color: red;
    border: none;
  }

  .title {
    margin-left: 10px;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    font-size: 15px;
    // margin-top: 5px;
  }
  .qian {
    margin-top: 10px;
    margin-left: 10px;
    color: #ff7600 !important;
    font-family: PingFang SC;
    font-size: 17px;
    font-weight: 600;
  }
  .kucun {
    margin-top: 2px;
    margin-left: 10px;
    color: #999;
  }
  p {
    margin: 6px 0;
    font-size: 14px;
    color: #999;
    padding: 0 14px;
  }
}
</style>